@(username: String)

$(function() {

	$(".btn").prop('disabled', true);
    var WS = window['MozWebSocket'] ? MozWebSocket : WebSocket
 	var chatSocket = new WS("@routes.Application.chat(username).webSocketURL(request)")
    
    
    $(".btn").click(function(){
    	var clickedID = this.id;
    	chatSocket.send(JSON.stringify(
            {name:"action",type:clickedID,specifier:$("#nr").val()}
        ))
        $(".btn").prop('disabled', true);
        //$("#exchange").prop('disabled', true);
    })
    
    var receiveEvent = function(event) {
        var data = JSON.parse(event.data)

        // Handle errors
        
        if(data.error) {
            chatSocket.close()
            $("#onError span").text(data.error)
            $("#onError").show()
            return
        }
        else if(data.permission) {
        	$("#onChat").show()
            $("#Actions").show()
            $(".btn").prop('disabled', true);
        	if(data.exchange) {
        		$("#exchange").prop('disabled', false);
        	}
        	else {
	        	if(data.check) {
	        		$("#check").prop('disabled', false);
	        	}
	        	if(data.bet) {
	        		$("#bet").prop('disabled', false);
	        	}
	        	if(data.raise) {
	        		$("#raise").prop('disabled', false);
	        	}
	        	if(data.call) {
	        		$("#call").prop('disabled', false);
	        	}
	        	if(data.fold) {
	        		$("#fold").prop('disabled', false);
	        	}
	        	if(data.allin) {
	        		$("#allin").prop('disabled', false);
	        	}
	        }
        }
        else if(data.cards) {
        	$("#onChat").show()
            $("#Actions").show()
            var image1 = document.getElementById('card1');
            var image2 = document.getElementById('card2');
            var image3 = document.getElementById('card3');
            var image4 = document.getElementById('card4');
        	var string = "@routes.Assets.at("images/favicon.gif")"
        	
        	image1.src = string.replace("favicon", data.cards.substring(0,2));
        	image2.src = string.replace("favicon", data.cards.substring(2,4));
        	image3.src = string.replace("favicon", data.cards.substring(4,6));
        	image4.src = string.replace("favicon", data.cards.substring(6,8));
        	$("#cards").text(data.cards)
        }
        else if(data.myCoins) {
        	$("#onChat").show()
            $("#Actions").show()
            $("#myCoins").text(data.myCoins)
        	if(data.myBet) {
        		$("#myBet").text(data.myBet)
        	}
        	if(data.stake) {
        		$("#stake").text(data.stake)
            	$("#maxBet").text(data.maxBet)
            }
        } 
        else if(data.stake) {
       		$("#stake").text(data.stake)
        	$("#maxBet").text(data.maxBet)
        }
        else {
        	$("#onChat").show()
            $("#Actions").show()   
           
        }
        
        if(data.winners) {
        	var win = "Winners: "+data.winners+"\n"+data.name1+": "+data.hand1+"\n"+data.name2+": "+data.hand2+"\n";
        	if(data.name3) {
        		win = win+data.name3+": "+data.hand3+"\n";
        	}
        	if(data.name4) {
        		win = win+data.name4+": "+data.hand4+"\n";
        	}
        	if(data.name5) {
        		win = win+data.name5+": "+data.hand5+"\n";
        	}
        	if(data.name6) {
        		win = win+data.name6+": "+data.hand6+"\n";
        	}
        	alert(win);
        	
        }
        
        if(data.message){      
	        var el = $('<div class="message"><p style="font-size:16px"></p></div>')
	        $("p", el).text(data.message)
	        $(el).addClass('me')
	        $('#messages').append(el)
	        
	    }
    }   



    chatSocket.onmessage = receiveEvent

})
